//
//  DAPopLikeAndCommentView.m
//  DAKnowledge
//
//  Created by DreamArts on 16/6/27.
//  Copyright © 2016年 lidongyang. All rights reserved.
//

#import "DAPopLikeAndCommentView.h"

#define kOperationViewSize CGSizeMake(120,34)
#define kAlbumSpatorY  5

@interface DAPopLikeAndCommentView ()
@property (nonatomic, strong) UIButton *replyButton;
@property (nonatomic, strong) UIButton *likeButton;
@property (nonatomic, strong) UILabel *lineLabel;
@property (nonatomic, assign) CGRect targetRect;
@end
@implementation DAPopLikeAndCommentView

+ (instancetype)initailzerDAOperationView{
    DAPopLikeAndCommentView *operationView = [[DAPopLikeAndCommentView alloc]initWithFrame:CGRectZero];
    return operationView;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.800];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 5.0;
        [self addSubview:self.likeButton];
        [self addSubview:self.replyButton];
        [self addSubview:self.lineLabel];
    }
    return self;
}

#pragma mark - Propertys
- (UIButton *)replyButton{
    if (!_replyButton) {
        _replyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _replyButton.tag = 1;
        [_replyButton addTarget:self action:@selector(operationDidClicked:) forControlEvents:UIControlEventTouchUpInside];
       _replyButton.frame = CGRectMake(CGRectGetMaxX(_likeButton.frame), 0, CGRectGetWidth(_likeButton. bounds), CGRectGetHeight(_likeButton.bounds));
        [_replyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _replyButton.titleLabel.font = [UIFont systemFontOfSize:12];
    }
    return _replyButton;
}

- (UIButton *)likeButton{
    if (!_likeButton) {
        _likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _likeButton.tag = 0;
        [_likeButton addTarget:self action:@selector(operationDidClicked:) forControlEvents:UIControlEventTouchUpInside];
         _likeButton.frame = CGRectMake(0, 0, kOperationViewSize.width / 2.0 , kOperationViewSize.height);
        
        [_likeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _likeButton.titleLabel.font = [UIFont systemFontOfSize:12];
    }
    return _likeButton;
}
- (UILabel *)lineLabel{
    if (!_lineLabel) {
        _lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.likeButton.frame.origin.x + self.likeButton.frame.size.width , 8, 0.5, self.likeButton.frame.size.height - 16)];
        _lineLabel.backgroundColor = [UIColor whiteColor];
    }
    return _lineLabel;
}

#pragma mark - Action
- (void)operationDidClicked:(UIButton *)sender{
    [self dismiss];
    if (self.didSelectedOperationCompletion) {
        self.didSelectedOperationCompletion(sender.tag);
    }
}

#pragma mark - 公开方法
- (void)showAtView:(UIView *)containerView rect:(CGRect)targetRect isFavour:(BOOL)isFavour{
    self.targetRect = targetRect;
    if (self.shouldShowed) {
        return;
    }
    [containerView addSubview:self];
    CGFloat width = kOperationViewSize.width;
    CGFloat height = kOperationViewSize.height;
    self.frame = CGRectMake(targetRect.origin.x, targetRect.origin.y - kAlbumSpatorY, 0, height);
    self.shouldShowed = YES;
    
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.frame = CGRectMake(targetRect.origin.x - width, targetRect.origin.y - kAlbumSpatorY, width, height);
    } completion:^(BOOL finished) {
        [_replyButton setTitle:@"评论" forState:UIControlStateNormal];
        [_likeButton setTitle:(isFavour ? @"取消赞": @"赞") forState:UIControlStateNormal];
        
    }];
    
}
- (void)dismiss{
    if (!self.shouldShowed) {
        return;
    }
    self.shouldShowed = NO;
    [UIView animateWithDuration:0.25 animations:^{
        [_replyButton setTitle:nil forState:UIControlStateNormal];
        [_likeButton setTitle:nil forState:UIControlStateNormal];
        CGFloat height = kOperationViewSize.height;
        self.frame = CGRectMake(self.targetRect.origin.x, self.targetRect.origin.y - kAlbumSpatorY, 0, height);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    } ];
    
    
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
